/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.lizg.View;

import cn.lizg.Model.Receive;
import cn.lizg.util.UiUtil;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author LizG
 */
public class SureRec_JFr extends javax.swing.JFrame {

    /**
     * Creates new form SureRec_JFr
     */
    private static String mess_sendip = null;
    private static String path = null;
    private static String filename = null;
    public SureRec_JFr() {
        initComponents();
        UiUtil.setFrameCenter(this);
        UiUtil.setFrameImage(this);
    }
      
    public SureRec_JFr(String mess_sendip,String path,String filename) {
        initComponents();
        UiUtil.setFrameCenter(this);
        UiUtil.setFrameImage(this);
        SureRec_JFr.mess_sendip = mess_sendip;
        SureRec_JFr.path = path;
        SureRec_JFr.filename = filename;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        surefile_jLb = new javax.swing.JLabel();
        sureRec_jBt = new javax.swing.JButton();
        cancelRec_jBt = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        sureRec_jBt.setText("确认接收");
        sureRec_jBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sureRec_jBtActionPerformed(evt);
            }
        });

        cancelRec_jBt.setText("取消接收");
        cancelRec_jBt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelRec_jBtActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(48, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(surefile_jLb, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(sureRec_jBt)
                        .addGap(53, 53, 53)
                        .addComponent(cancelRec_jBt)))
                .addGap(38, 38, 38))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(surefile_jLb, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(sureRec_jBt))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(60, 60, 60)
                        .addComponent(cancelRec_jBt)))
                .addContainerGap(73, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sureRec_jBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sureRec_jBtActionPerformed
       // TODO add your handling code here:
            System.out.println("sure不可见");
            this.setVisible(false);
        try {
            DatagramSocket ds;
            DatagramPacket dp;
            String mess = "@receive";
            ds = new DatagramSocket();
            dp = new DatagramPacket(mess.getBytes(), mess.getBytes().length,
                    InetAddress.getByName(mess_sendip), 8189);
            System.out.println("ip:" +mess_sendip);
            ds.send(dp);
            Receive receive = new Receive();
            //设置Path
            receive.setFile(path+"\\" + filename);
            receive.receive();
            JOptionPane.showMessageDialog(this, "传输完成完成");
            // 发送数据
            
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("SureRec_JFr116");
        }finally{
            //System.gc();
            this.dispose();
        }
    }//GEN-LAST:event_sureRec_jBtActionPerformed

    private void cancelRec_jBtActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelRec_jBtActionPerformed
        // TODO add your handling code here:
        Receive_JFr.cancle = true;
        this.dispose();
    }//GEN-LAST:event_cancelRec_jBtActionPerformed

    /**
     * @param flag
     */
    public void setSure_JFrV(boolean flag) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SureRec_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SureRec_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SureRec_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SureRec_JFr.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>
        
        surefile_jLb.setText("对方发来" + filename + "文件");

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new SureRec_JFr().setVisible(flag);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelRec_jBt;
    private javax.swing.JButton sureRec_jBt;
    private javax.swing.JLabel surefile_jLb;
    // End of variables declaration//GEN-END:variables
}
